package en.tedu.egmvc04.controller;

import en.tedu.egmvc04.mapper.AddressMapper;
import en.tedu.egmvc04.pojo.dto.AddressDto;
import en.tedu.egmvc04.pojo.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("address/")
public class AddressController {
    @Autowired(required = false)
    private AddressMapper addressMapper;

    @RequestMapping("insert")
    public String insertAddress(AddressDto addressDto){
        int i = addressMapper.insertAddress(addressDto);
        if (i==1){
            return "增加地址成功！";
        }
        return "服务器繁忙!";
    }



    @RequestMapping("delete")
    public String deleteAddress(Integer id){
        if (addressMapper.selectOneAddress(id)!=null){
            int i = addressMapper.deleteAddress(id);
            if (i==1){
                return "删除地址成功！";
            }
            return "服务器繁忙!";
        }
        return "没有此id的地址,无法删除!";
    }




    @RequestMapping("update")
    public String updateAddress(Address address){
        if (addressMapper.selectOneAddress(address.getId())!=null){
            int i = addressMapper.updateAddress(address);
            if (i==1){
                return "更新地址成功！";
            }
            return "服务器繁忙!";
        }
        return "此id的地址不存在，无法修改!";
    }




    @RequestMapping("selectAll")
    public List<Address> selectAllAddress(){
        return addressMapper.selectAllAddress();
    }




    @RequestMapping("selectone")
    public Address selectOneAddress(Integer id){
        Address address=addressMapper.selectOneAddress(id);
        if (address!=null)
        return address;
        return null;
    }



// <!--    int insertAddress(AddressDto addressDto);-->
//
//<!--    int deleteAddress(Integer id);-->
//
//<!--    int updateAddress(Address address);-->
//
//<!--    List<Address> selectAllAddress();-->
//
//<!--    Address selectOneAddress(Integer id);-->
}
